home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1122 / 1122.xpi / chrome / tabmixplus.jar / content / tabmixplus / pref / pref-tabmix.js < prev    next >
Text File  |  2009-10-11  |  40KB  |  1,027 lines

  1. /***** Preference Dialog Functions *****/
  2. const Cc = Components.classes;
  3. const Ci = Components.interfaces;
  4. const pBranch = Ci.nsIPrefBranch;
  5. var gTabmixPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2);
  6. var nsIPrefServiceObj = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
  7. var winMediator= Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
  8.  
  9. var gPrefs;
  10.  
  11. function before_Init() {
  12.   if (/^Mac/.test(navigator.platform)) {
  13.     var label = document.getElementById("tabId").getAttribute("label2");
  14.     document.getElementById("tabId").setAttribute("label", label);
  15.   }
  16.  
  17.   var topWindow = getMostRecentWindow();
  18.   if (!topWindow.gIsFirefox35) {
  19.     var keepWindow = document.getElementById("keepWindow");
  20.     keepWindow.setAttribute("label", keepWindow.getAttribute("label3"));
  21.     // From Firefox 3.5+ we can close last tab even when the tabbar is hidden
  22.     // in Firefox 3.0 we use observes to disable this if we hide the tabbar
  23.     document.getElementById("keepLastTab").setAttribute("observes", "obs_hideTabbar");
  24.   }
  25.  
  26.   var ctrlTab = topWindow.document.getElementById("ctrlTab-panel") && "ctrlTab" in topWindow;
  27.   if (!ctrlTab) {
  28.      var tabPreviews = document.getElementById("ctrltab.tabPreviews");
  29.      tabPreviews.parentNode.removeChild(tabPreviews);
  30.   }
  31.  
  32.   /* Chromifox theme force button height to 25px */
  33.   var skin = gTabmixPrefs.getCharPref("general.skins.selectedSkin");
  34.   if (skin == "cfxec")
  35.     document.getElementById("pref-tabmix").setAttribute("chromifox", true);
  36. }
  37.  
  38. // load all preferences into the dialog
  39. function TM_EMinit() {
  40.   var browserWindow = getMostRecentWindow();
  41.   toolbarButtons(browserWindow);
  42.   gPrefs =  document.getElementsByAttribute("prefstring", "*");
  43.  
  44.   getTab();
  45.  
  46.   // show groupbox if incompatible extensions exist in this profile
  47.   if (browserWindow.getExtensions().length == 0)
  48.     document.getElementById("incompatible").collapsed = true;
  49.  
  50.   // add EventListener when we start
  51.   window.addEventListener("unload", setLastTab, false);
  52.   window.addEventListener("command", TM_enableApply, false);
  53.   window.addEventListener("input", TM_enableApply, false);
  54.  
  55.   // update tabclicking items that aren't change by tabmix
  56.   TM_Options.setItem("snapBack", "hidden", !(browserWindow.SessionSaver && browserWindow.SessionSaver.snapBackTab));
  57.   var isIE = (browserWindow.IeView && browserWindow.IeView.ieViewLaunch) ||
  58.              (browserWindow.gIeTab && browserWindow.gIeTab.switchTabEngine) ||
  59.              (browserWindow.ieview && browserWindow.ieview.launch);
  60.   TM_Options.setItem("ieView", "hidden", !isIE);
  61.  
  62.   // check if bookmark item in tab context menu
  63.   TM_Options.setItem("bmMenu", "hidden", !(browserWindow.document.getElementById("context_bookmarkAllTabs")));
  64.   // check if  "Browser:BookmarkAllTabs" command exist
  65.   TM_Options.setItem("bmTabsCommand", "hidden", !(browserWindow.document.getElementById("Browser:BookmarkAllTabs")));
  66.  
  67.   // disable TMP session manager setting if session manager extension is install
  68.   if ("gSessionManager" in browserWindow) {
  69.     document.getElementById("sessionmanager_ext").hidden = false;
  70.     document.getElementById("paneSession-tabbox").selectedIndex = 0;
  71.     document.getElementById("chooseFile").selectedIndex = 1;
  72.   }
  73.   else {
  74.     // create saved Session popup menu
  75.     var popup = document.getElementById("onStart.popup");
  76.     SessionManager.createMenuForDialog(popup);
  77.   }
  78.  
  79.   // disable options for position the tabbar and scroll mode if TreeStyleTab extension installed
  80.   if ("TreeStyleTabBrowser" in browserWindow) {
  81.     TM_Options.setItem("tabBarPosition", "disabled", true);
  82.     TM_Options.setItem("tabBarPosition.label", "disabled", true);
  83.     TM_Options.setItem("tabScroll", "disabled", true);
  84.     TM_Options.setItem("tabScroll.label", "disabled", true);
  85.   }
  86.  
  87.   // hide newTabButton.posiotion.afterlast for version prior Firefox 3.5
  88.   if (!browserWindow.gIsFirefox35) {
  89.     var afterLast = document.getElementById("newTabButton.posiotion.afterlast");
  90.     var rigthSide = document.getElementById("newTabButton.posiotion.right");
  91.     rigthSide.hidden = true;
  92.     afterLast.setAttribute("label", rigthSide.getAttribute("label"));
  93.   }
  94.  
  95.   if (!browserWindow.gIsFirefox36) {
  96.     document.getElementById("allTabsPpreviews").setAttribute("hidden", true);
  97.   }
  98.  
  99.   // Init tabclicking options
  100.   var menuPopup = document.getElementById("ClickTab").firstChild;
  101.   document.getElementById("ClickTabbar").appendChild(menuPopup.cloneNode(true));
  102.  
  103.   // verify that all the prefs exist .....
  104.   browserWindow.gTMPprefObserver.addMissingPrefs();
  105.  
  106.   TM_setElements(false, true);
  107.   // check if apply is on
  108.   // apply changes if we set single window mode status
  109.   TM_EMsave();
  110.  
  111.   var tabclicking = document.getElementById("tabclicking_tabs");
  112.   // change tab label on Mac. trigger onselect before broadcaster is set
  113.   // so we add the onselect here
  114.   tabclicking.addEventListener("select", tabSelectionChanged, false);
  115.  
  116.   // for locals with long labels
  117.   var hbox = document.getElementById("focusTab-box");
  118.   var label = document.getElementById("focusTab-label").boxObject.width;
  119.   var menulist = document.getElementById("focusTab");
  120.   if (hbox.boxObject.width > label + menulist.boxObject.width) {
  121.     menulist.parentNode.removeAttribute("pack");
  122.     hbox.setAttribute("orient", "horizontal");
  123.     hbox.setAttribute("align","center");    
  124.   }
  125.  
  126.   hbox = document.getElementById("tabScroll-box");
  127.   label = document.getElementById("tabScroll.label").boxObject.width;
  128.   var menulist = document.getElementById("tabScroll");
  129.   var ident = 23; // we have class="ident"
  130.   if (hbox.boxObject.width > label + menulist.boxObject.width - ident) {
  131.     menulist.parentNode.removeAttribute("pack");
  132.     menulist.parentNode.removeAttribute("class");
  133.     hbox.setAttribute("orient", "horizontal");
  134.     hbox.setAttribute("align","center");    
  135.   }
  136.  
  137.   hbox = document.getElementById("progressMeter-box");
  138.   var progressMeterv = document.getElementById("progressMeter").boxObject.width;
  139.   var noprogress = document.getElementById("noprogress");
  140.   if (hbox.boxObject.width > progressMeterv + noprogress.boxObject.width) {
  141.     noprogress.removeAttribute("class");
  142.     hbox.setAttribute("orient", "horizontal");
  143.   }
  144.  
  145.   if (/^Linux/.test(navigator.platform))
  146.      sizeToContent();
  147.  
  148.   window.setTimeout( function () { window.focus();}, 0 );
  149. }
  150.  
  151. // save all preferences entered into the dialog
  152. function TM_EMsave(onApply) {
  153.   // we only need to save if apply is enabled
  154.   if (!onApply && document.documentElement.getButton("extra1").disabled)
  155.     return true;
  156.  
  157.   // set flag to prevent TMupdateSettings from run for each change
  158.   gTabmixPrefs.setBoolPref("extensions.tabmix.setDefault", true);
  159.  
  160.   TM_Options.singleWindow( document.getElementById("singleWindow").checked );
  161.   TM_Options.setTabXUI();
  162.   TM_verifyWidth();
  163.   TM_Options.verify_PostDataBytes();
  164.  
  165.   document.documentElement.getButton("accept").focus();
  166.   document.documentElement.getButton("extra1").disabled = true;
  167.  
  168.   if (!("undefined" in applyData)) {
  169.     for (var _pref in applyData)
  170.       setPrefByType(_pref, applyData[_pref]);
  171.   }
  172.   else {
  173.     // this part is only if the applayData fail for some unknown reason
  174.     // we don't supposed to get here
  175.     for (var i = 0; i < gPrefs.length; ++i )
  176.       setPrefByType(gPrefs[i].getAttribute("prefstring"), getValue(gPrefs[i]));
  177.   }
  178.  
  179.   // set saved sessionpath if loadsession >=0
  180.   var TMP_manager_enabled = gTabmixPrefs.getBoolPref("extensions.tabmix.sessions.manager");
  181.   if (TMP_manager_enabled) {
  182.     var val = gTabmixPrefs.getIntPref("extensions.tabmix.sessions.onStart.loadsession");
  183.     var popup = document.getElementById("onStart.popup");
  184.     var pref = "extensions.tabmix.sessions.onStart.sessionpath";
  185.     gTabmixPrefs.setCharPref(pref, popup.getElementsByAttribute("value", val)[0].getAttribute("session"));
  186.   }
  187.  
  188.   applyData = [];
  189.   gTabmixPrefs.clearUserPref("extensions.tabmix.setDefault"); // this trigger TMupdateSettings
  190.   TM_Options.isSessionStoreEnabled(true);
  191.  
  192.   callUpdateSettings();
  193.  
  194.   nsIPrefServiceObj.savePrefFile(null); // store the pref immediately
  195.   return true;
  196. }
  197.  
  198. function callUpdateSettings() {
  199.   var pref = "extensions.tabmix.PrefObserver.error";
  200.   if (gTabmixPrefs.prefHasUserValue(pref) && gTabmixPrefs.getBoolPref(pref)) {
  201.     var wnd, enumerator = winMediator.getEnumerator("navigator:browser");
  202.     while (enumerator.hasMoreElements()) {
  203.       wnd = enumerator.getNext();
  204.       wnd.TMupdateSettings();
  205.     }
  206.   }
  207. }
  208.  
  209. function TM_verifyWidth() {
  210.    var minWidth = document.getElementById("minWidth");
  211.    var maxWidth = document.getElementById("maxWidth");
  212.  
  213.    var minValue = minWidth.value;
  214.    var maxValue = maxWidth.value;
  215.  
  216.    if (maxValue - minValue < 0) {
  217.       minWidth.value = maxValue;
  218.       maxWidth.value = minValue;
  219.    }
  220.  
  221.    if (minWidth.value < 22)
  222.      minWidth.value = 22;
  223.  
  224.    if (minValue != minWidth.value)
  225.       updateApplyData(minWidth);
  226.  
  227.    if (maxValue != maxWidth.value)
  228.       updateApplyData(maxWidth);
  229.  
  230. }
  231.  
  232. var TM_Options = {
  233.    checkDependant: function(start) {
  234.  
  235.       this.singleWindow( document.getElementById("singleWindow").checked );
  236.  
  237.       var broadcasters = document.getElementById("disabled:Broadcaster");
  238.       for (var i = 0; i < broadcasters.childNodes.length; ++i ) {
  239.          var _id = broadcasters.childNodes[i].id.replace("obs_", "");
  240.          this.disabled(_id, start);
  241.       }
  242.  
  243.       this.setTabXUI();
  244.       this.addTabXUI();
  245.       this.selectTab();
  246.       this.tabScroll();
  247.       this.speLink();
  248.       this.newTabUrl(document.getElementById("loadOnNewTab"), false, false);
  249.       this.setShowTabList();
  250.       this.setDisabeled_replaceLastTabWith();
  251.       this.isSessionStoreEnabled(false);
  252.  
  253.       this.setDisabled("obs_ss_postdata", document.getElementById("ss_postdata").value == 2);
  254.       this.setDisabled("obs_hideTabbar", document.getElementById("hideTabbar").value != 0);
  255.    },
  256.  
  257.    disabled: function(itemOrId, start) {
  258.       var item = typeof(itemOrId) == "string" ? document.getElementById(itemOrId) : itemOrId;
  259.       var val = item.getAttribute("inverseDependency") ? item.checked : !item.checked;
  260.       if (start && !val)
  261.          return;
  262.       this.setDisabled("obs_" + item.id, val);
  263.    },
  264.  
  265.    setDisabled: function(id, val) {
  266.       if (val == true)
  267.          this.setItem(id, "disabled" , val);
  268.       else {
  269.          // remove disabled from all observers,
  270.          // we can't edit textbox-input with disabled=null or disabled=false
  271.          // textbox-input inherits the dislabled attribute from the textbox
  272.  
  273.          // all broadcaster has no disabled attribute at startup
  274.          var aBroadcaster = document.getElementById(id);
  275.          if (aBroadcaster.hasAttribute("disabled")) {
  276.             aBroadcaster.removeAttribute("disabled");
  277.          }
  278.       }
  279.    },
  280.  
  281.    newTabUrl: function(item, disable, setFocus) {
  282.       var showTabUrlBox = item.selectedItem.value == 4;
  283.       var idnum = item.getAttribute("idnum") || "" ;
  284.       this.setDisabled("newTabUrlLabel" + idnum, !showTabUrlBox || disable);
  285.       this.setDisabled("newTabUrl" + idnum, !showTabUrlBox || disable);
  286.       if (setFocus && showTabUrlBox)
  287.          document.getElementById("newTabUrl" + idnum).focus();
  288.    },
  289.  
  290.    setDisabeled_replaceLastTabWith: function() {
  291.       // we disable replaceLastTabWith if one of this test is true
  292.       // browser.tabs.closeWindowWithLastTab == true OR
  293.       // extensions.tabmix.keepLastTab = true OR
  294.       // extensions.tabmix.hideTabbar != 0
  295.       // when we enable the item we need to set the disable state for newTabUrl_1
  296.       var closeWindow = !document.getElementById("keepWindow").checked // inverted pref;
  297.       var keepLastTab = document.getElementById("keepLastTab").checked
  298.       var hideTabbar;
  299.       if (!getMostRecentWindow().gIsFirefox35)
  300.         hideTabbar = document.getElementById("hideTabbar").value != 0;
  301.       var disable = closeWindow || keepLastTab || hideTabbar;
  302.       this.setDisabled("obs_replaceLastTabWith", disable);
  303.       this.newTabUrl(document.getElementById("replaceLastTabWith"), disable, !disable);
  304.    },
  305.  
  306.    addTabXUI: function() {
  307.       var tabXValue = document.getElementById("addTabXUI").selectedItem.value;
  308.       this.setItem("tabXdelaycheck", "hidden", tabXValue != 2 && tabXValue != 4);
  309.       this.setItem("tabXwidthBox", "hidden", tabXValue != 5);
  310.    },
  311.  
  312.    setTabXUI: function() {
  313.       if (document.getElementById("flexTabs").checked) {
  314.          var tabXUI = document.getElementById("addTabXUI");
  315.          if ( tabXUI.selectedItem.value == 5) {
  316.             updateApplyData(tabXUI, 1);
  317.             this.addTabXUI();
  318.          }
  319.       }
  320.    },
  321.  
  322.    selectTab: function() {
  323.       var focusType = document.getElementById("selectTab").checked ? "bg":"fg";
  324.       var val = document.getElementById("showInverseLink").getAttribute(focusType+"label");
  325.       this.setItem("showInverseLink", "label", val);
  326.    },
  327.  
  328.    tabScroll: function() {
  329.       var selectedValue = document.getElementById("tabScroll").value;
  330.       var vis = selectedValue == 2 ? "visibility: visible" : "visibility: hidden";
  331.       this.setItem("maxbar", "style", vis);
  332.    },
  333.  
  334.    speLink: function() {
  335.       var spelink = document.getElementById("speLink").selectedItem.value;
  336.       document.getElementById("inverselinks").disabled =  spelink != 2 && document.getElementById("midcurrent").checked;
  337.    },
  338.  
  339.    singleWindow: function(enableSingleWindow) {
  340.       if ( enableSingleWindow ) {
  341.          function updateStatus(itemId, testVal, test, newVal) {
  342.             var item = document.getElementById(itemId);
  343.             test = test ? item.value == testVal : item.value != testVal
  344.             if ( test ) {
  345.                updateApplyData(item, newVal);
  346.             }
  347.          }
  348.  
  349.          updateStatus("externalLinkTarget", 2, true, 3);
  350.          updateStatus("divertedWindowOpen", 0, false, 0);
  351.          updateStatus("generalWindowOpen", 2, true, 3);
  352.       }
  353.    },
  354.  
  355.    verify_PostDataBytes: function() {
  356.       var ss_postdatabytes = document.getElementById("ss_postdatabytes");
  357.       var val = ss_postdatabytes.value;
  358.       if (val == "-" || val == "") {
  359.          updateApplyData(ss_postdatabytes, val == "" ? "0" : "-1");
  360.       }
  361.    },
  362.  
  363.    isSessionStoreEnabled: function (checkService) {
  364.       var browserWindow = getMostRecentWindow();
  365.       if (checkService)
  366.         browserWindow.TMP_SessionStore.setService(2, false, window);
  367.  
  368.       if ("gSessionManager" in browserWindow)
  369.         return;
  370.  
  371.       var sessionStoreEnabled = browserWindow.TMP_SessionStore.isSessionStoreEnabled();
  372.       var currentState = document.getElementById("sessionstore_0").checked;
  373.       if (currentState != sessionStoreEnabled || (!checkService && !sessionStoreEnabled)) {
  374.         document.getElementById("sessionstore_0").checked = sessionStoreEnabled;
  375.         document.getElementById("sessionstore_1").checked = sessionStoreEnabled;
  376.         document.getElementById("paneSession-tabbox").selectedIndex = sessionStoreEnabled ? 1 : 2;
  377.       }
  378.    },
  379.  
  380.    setSessionsOptions: function (item, id) {
  381.       var useSessionManager = !item.checked;
  382.       document.getElementById("paneSession-tabbox").selectedIndex = item.checked ? 1 : 2;
  383.       document.getElementById(id).checked = item.checked;
  384.       document.getElementById(id).focus();
  385.  
  386.       function updateSessionPrefs(aItemId, aValue) {
  387.          var item = document.getElementById(aItemId);
  388.          updateApplyData(item, aValue);
  389.       }
  390.  
  391.       // TMP session pref
  392.       updateSessionPrefs("sessionManager", useSessionManager);
  393.       updateSessionPrefs("sessionCrashRecovery", useSessionManager);
  394.  
  395.       // sessionstore pref
  396.       updateSessionPrefs("browser.warnOnRestart", !useSessionManager);
  397.       updateSessionPrefs("browser.warnOnQuit", !useSessionManager);
  398.       updateSessionPrefs("resume_from_crash", !useSessionManager);
  399.       // "browser.startup.page"
  400.       updateSessionPrefs("browserStartupPage", useSessionManager ? 1 : 3);
  401.    },
  402.  
  403.    setUndoCloseCache: function (item) {
  404.       var undoCloseCache = document.getElementById("undoCloseCache");
  405.       var currentValue = undoCloseCache.value;
  406.       var newValue = item.checked ? 10 : 0;
  407.       if (newValue != currentValue) {
  408.         updateApplyData(undoCloseCache, newValue);
  409.       }
  410.    },
  411.  
  412.    setUndoClose: function (item) {
  413.       if (item.value == "")
  414.         item.value = 0;
  415.  
  416.       if (item.value == 0) {
  417.         var undoClose = document.getElementById("undoClose");
  418.         undoClose.checked = false;
  419.         this.disabled(undoClose);
  420.         this.setUndoCloseCache(undoClose);
  421.       }
  422.    },
  423.  
  424.    setShowTabList: function () {
  425.       // we only need this in Firefox 3.5+
  426.       if (document.getElementById("ctrltab.tabPreviews") == null)
  427.          return;
  428.  
  429.       var disableShowTabList = document.getElementById("ctrltab").checked &&
  430.                                 document.getElementById("ctrltab.tabPreviews").checked;
  431.       this.setDisabled("showTabList", disableShowTabList);
  432.       if (!document.getElementById("obs_showTabList").hasAttribute("disabled"))
  433.         this.setDisabled("respondToMouse", disableShowTabList);
  434.    },
  435.  
  436.    // Set given attribute of specified item.
  437.    // If the value is null, then it removes the attribute
  438.    // (which works nicely for the disabled attribute).
  439.    setItem: function (id, attrib, val) {
  440.       var item = document.getElementById(id);
  441.       if (val == null) {
  442.         item.removeAttribute(attrib);
  443.         return;
  444.       }
  445.  
  446.       if (typeof(val) == "boolean")
  447.         val = val ? "true" : "false";
  448.  
  449.       if (item.getAttribute(attrib) != val)
  450.         item.setAttribute(attrib, val);
  451.    }
  452.  
  453. }
  454.  
  455. // other settings not in the main option dialog
  456. var otherPref = ["sessions.onStart.sessionpath","unreadTabreload","reload_time","custom_reload_time",
  457.                   "filetype","sessions.menu.showext","disableIncompatible","hideIcons","disableF9Key",
  458.                   "styles.currentTab","styles.unreadTab","styles.otherTab","styles.progressMeter"];
  459.  
  460. function TM_defaultSetting () {
  461.   // set flag to prevent TMupdateSettings from run for each change
  462.   gTabmixPrefs.setBoolPref("extensions.tabmix.setDefault", true);
  463.  
  464.   TM_setElements(true);
  465.  
  466.   TM_disableApply();
  467.  
  468.   // reset other settings to default
  469.   var tmpPref = "extensions.tabmix.";
  470.   for (var i = 0; i < otherPref.length; ++i )
  471.     if (gTabmixPrefs.prefHasUserValue(tmpPref+otherPref[i])) gTabmixPrefs.clearUserPref(tmpPref+otherPref[i]);
  472.  
  473.   gTabmixPrefs.clearUserPref("extensions.tabmix.setDefault");
  474.   TM_Options.isSessionStoreEnabled(true);
  475.   callUpdateSettings();
  476.  
  477.   nsIPrefServiceObj.savePrefFile(null); // store the pref immediately
  478. }
  479.  
  480. function getPrefByType(prefName) {
  481.    try {
  482.       switch (gTabmixPrefs.getPrefType(prefName)) {
  483.          case pBranch.PREF_BOOL:
  484.             return gTabmixPrefs.getBoolPref(prefName);
  485.          case pBranch.PREF_INT:
  486.             return gTabmixPrefs.getIntPref(prefName);
  487.          case pBranch.PREF_STRING:
  488.             return gTabmixPrefs.getCharPref(prefName);
  489.       }
  490.    } catch (ex) {TMP_ASSERT(ex, "error in getPrefByType " + "\n" + "caller " + getPrefByType.caller.name + "\n"+ prefName);}
  491.    return null;
  492. }
  493.  
  494. function setPrefByType(prefName, newValue, atImport) {
  495.    try {
  496.       switch (gTabmixPrefs.getPrefType(prefName)) {
  497.          case pBranch.PREF_BOOL:
  498.             if (atImport) {
  499.                newValue = /true/i.test(newValue);
  500.                // from tabmix 0.3.6.0.080223 we use extensions.tabmix.hideTabbar
  501.                if (prefName == "browser.tabs.autoHide") {
  502.                   newValue = newValue ? 1 : 0;
  503.                   gTabmixPrefs.setIntPref("extensions.tabmix.hideTabbar", newValue);
  504.                   return;
  505.                }
  506.             }
  507.             gTabmixPrefs.setBoolPref(prefName, newValue);
  508.             break;
  509.          case pBranch.PREF_INT:
  510.             gTabmixPrefs.setIntPref(prefName, newValue);
  511.             break;
  512.          case pBranch.PREF_STRING:
  513.             // in prev version we use " " for to export string to file
  514.             if (atImport && newValue.indexOf('"') == 0)
  515.                newValue = newValue.substring(1,newValue.length-1);
  516.             gTabmixPrefs.setCharPref(prefName, newValue);
  517.             break;
  518.          default:
  519.             if (!atImport)
  520.                break;
  521.             // when we import from old saved file, we need to replace old pref that are not in use.
  522.             switch (prefName) {
  523.                // in 0.3.0.605 we changed tab color from old pref to new pref
  524.                // old pref "extensions.tabmix.currentColor" type integer
  525.                // new pref "extensions.tabmix.currentColorCode" type string
  526.                case "extensions.tabmix.currentColor":
  527.                case "extensions.tabmix.unreadColor":
  528.                case "extensions.tabmix.progressColor":
  529.                   var colorCodes = ["#CF1919", "#0E36EF", "#DDDF0D", "#3F8F3E", "#E066FF", "#86E7EF",
  530.                                    "#FFFFFF", "#7F7F7F", "#000000", "#EF952C", "#FF82AB", "#7F4C0F", "#AAAAFF"]
  531.                   newValue = colorCodes[newValue];
  532.                   prefName = prefName + "Code";
  533.                // in 0.3.7.4 2008-12-24 we combined all style pref into one per type
  534.                // extensions.tabmix.styles.[TYPE NAME]
  535.                case "extensions.tabmix.boldUnread":
  536.                case "extensions.tabmix.italicUnread":
  537.                case "extensions.tabmix.underlineUnread":
  538.                case "extensions.tabmix.boldCurrent":
  539.                case "extensions.tabmix.italicCurrent":
  540.                case "extensions.tabmix.underlineCurrent":
  541.                case "extensions.tabmix.unreadColorCode":
  542.                case "extensions.tabmix.currentColorCode":
  543.                case "extensions.tabmix.progressColorCode":
  544.                case "extensions.tabmix.useCurrentColor":
  545.                case "extensions.tabmix.useUnreadColor":
  546.                case "extensions.tabmix.useProgressColor":
  547.                   var pref = prefName.toLowerCase().replace(/extensions.tabmix.|color/g,"")
  548.                                           .replace(/italic|bold|underline/g, ",$&,")
  549.                                           .replace("use", ",text,")
  550.                                           .replace("code", ",textColor,")
  551.                                           .split(",");
  552.                   var styleName, attrib;
  553.                   [styleName, attrib] = prefName.indexOf("Code") > -1 ? [pref[0], pref[1]] : [pref[2], pref[1]];
  554.                   if (styleName == "progress") {
  555.                     attrib = attrib.replace("text", "bg");
  556.                     styleName += "Meter"
  557.                   }
  558.                   else
  559.                     styleName += "Tab";
  560.                   oldStylePrefs[styleName][attrib] = newValue;
  561.                   oldStylePrefs.found = true;
  562.                   break;
  563.                // changed at 2008-02-26
  564.                case "extensions.tabmix.undoCloseCache":
  565.                   gTabmixPrefs.setIntPref("browser.sessionstore.max_tabs_undo", newValue);
  566.                   break;
  567.                // changed at 2008-08-17
  568.                case "extensions.tabmix.opentabfor.search":
  569.                   gTabmixPrefs.setBoolPref("browser.search.openintab", /true/i.test(newValue));
  570.                   break;
  571.                // changed at 2008-09-23
  572.                case "extensions.tabmix.keepWindow":
  573.                   gTabmixPrefs.setBoolPref("browser.tabs.closeWindowWithLastTab", !(/true/i.test(newValue)));
  574.                   break;
  575.                // changed at 2008-09-28
  576.                case "browser.ctrlTab.mostRecentlyUsed":
  577.                case "extensions.tabmix.lasttab.handleCtrlTab":
  578.                   gTabmixPrefs.setBoolPref("browser.ctrlTab.previews", /true/i.test(newValue));
  579.                   break;
  580.                // 2008-11-29
  581.                case "extensions.tabmix.maxWidth":
  582.                   gTabmixPrefs.setIntPref("browser.tabs.tabMaxWidth", newValue);
  583.                   break;
  584.                // 2008-11-29
  585.                case "extensions.tabmix.minWidth":
  586.                   gTabmixPrefs.setIntPref("browser.tabs.tabMinWidth", newValue);
  587.                   break;
  588.                // 2009-01-31
  589.                case "extensions.tabmix.newTabButton.leftside":
  590.                   gTabmixPrefs.setIntPref("extensions.tabmix.newTabButton.position", /true/i.test(newValue) ? 0 : 2);
  591.                   break;
  592.                // 2009-10-10
  593.                case "extensions.tabmix.windows.warnOnClose":
  594.                   gTabmixPrefs.setBoolPref("extensions.tabmix.tabs.warnOnClose", gTabmixPrefs.getBoolPref("browser.tabs.warnOnClose"));
  595.                   gTabmixPrefs.setBoolPref("browser.tabs.warnOnClose", /true/i.test(newValue));
  596.                   break;                  
  597.             }
  598.       }
  599.    } catch (ex) {TMP_ASSERT(ex, "error in setPrefByType " + "\n" + "caller " + setPrefByType.caller.name + "\n"+ prefName + "\n" + newValue);}
  600. }
  601.  
  602. function TM_setElements (restore, start) {
  603.    for (var i = 0; i < gPrefs.length; ++i ) {
  604.       var item = gPrefs[i];
  605.       var pref = item.getAttribute("prefstring");
  606.  
  607.       if (restore) {
  608.         switch (pref) {
  609.            case "browser.tabs.closeWindowWithLastTab": // exist in firefox version 3.5
  610.               gTabmixPrefs.setBoolPref(pref, false);
  611.               break;
  612.            case "browser.ctrlTab.previews": // exist in firefox version 3.5
  613.               gTabmixPrefs.setBoolPref(pref, true);
  614.               break;
  615.            case "browser.link.open_external": // not exist from firefox 3.5
  616.              gTabmixPrefs.setIntPref(pref, 3);
  617.               break;
  618.            default:
  619.              if (gTabmixPrefs.prefHasUserValue(pref))
  620.                gTabmixPrefs.clearUserPref(pref);
  621.         }
  622.       }
  623.  
  624.       var prefValue = getPrefByType(pref);
  625.       switch (item.localName) {
  626.          case "checkbox":
  627.             if (item.hasAttribute("inverted"))
  628.                prefValue = !prefValue;
  629.             item.checked = prefValue;
  630.             break;
  631.          case "radiogroup":
  632.             if (item.hasAttribute("boolean"))
  633.                prefValue = prefValue ? 0 : 1;
  634.             item.selectedIndex = prefValue;
  635.             break;
  636.          default:
  637.             item.value = prefValue;
  638.       }
  639.    }
  640.  
  641.    setSelectedIndex(document.getElementById("tabclicking_tabs").selectedIndex);
  642.    TM_Options.checkDependant(start);
  643. }
  644.  
  645. function exportData() {
  646.  
  647.   TM_EMsave();
  648.  
  649.   var patterns = new Array;
  650.   patterns[0] = "tabmixplus";
  651.   var z = 1, pref;
  652.  
  653.   for (var i = 0; i < gPrefs.length; ++i ) {
  654.      pref = gPrefs[i].getAttribute("prefstring");
  655.      patterns[z++] = pref + "=" + getPrefByType(pref);
  656.   }
  657.  
  658.   // more pref to save
  659.   for (i = 0; i < otherPref.length; ++i ){
  660.     pref = "extensions.tabmix." + otherPref[i];
  661.     patterns[z++] = pref + "=" + getPrefByType(pref);
  662.   }
  663.  
  664.   saveToFile(patterns);
  665.   return true;
  666. }
  667.  
  668. function saveToFile (patterns) {
  669.   // thanks to adblock
  670.   var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  671.   var stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
  672.  
  673.   fp.init(window, null, fp.modeSave);
  674.   fp.defaultExtension = "txt";
  675.   fp.defaultString = "TMPpref";
  676.   fp.appendFilters(fp.filterText);
  677.  
  678.   if (fp.show() != fp.returnCancel) {
  679.     if (fp.file.exists()) fp.file.remove(true);
  680.     fp.file.create(fp.file.NORMAL_FILE_TYPE, 0666);
  681.     stream.init(fp.file, 0x02, 0x200, null);
  682.  
  683.     for (var i = 0; i < patterns.length ; i++) {
  684.       patterns[i]=patterns[i]+"\n";
  685.       stream.write(patterns[i], patterns[i].length);
  686.     }
  687.  
  688.     stream.close();
  689.   }
  690. }
  691.  
  692. var oldStylePrefs = {currentTab: {}, unreadTab: {}, progressMeter: {}, found: false};
  693. function importData () {
  694.    var pattern = loadFromFile();
  695.  
  696.    if (!pattern) return false;
  697.    var i, stringBundle;
  698.    if(pattern[0]!="tabmixplus") {
  699.       stringBundle = document.getElementById("tmp-string-bundle");
  700.       //  Can not import because it is not a valid file.
  701.       alert(stringBundle.getString("tmp.importPref.error1"));
  702.       return false;
  703.    }
  704.  
  705.    // set flag to prevent TMupdateSettings from run for each change
  706.    gTabmixPrefs.setBoolPref("extensions.tabmix.setDefault", true);
  707.    var prefName, prefValue;
  708.    for (i=1; i<pattern.length; i++){
  709.       var index = pattern[i].indexOf("=");
  710.       if (index > 0){
  711.          prefName  = pattern[i].substring(0,index);
  712.          prefValue = pattern[i].substring(index+1,pattern[i].length);
  713.          setPrefByType(prefName, prefValue, true);
  714.       }
  715.    }
  716.    if (oldStylePrefs.found) {
  717.       var browserWindow = getMostRecentWindow();
  718.       browserWindow.gTMPprefObserver.converOldStylePrefs("currentTab", oldStylePrefs.currentTab);
  719.       browserWindow.gTMPprefObserver.converOldStylePrefs("unreadTab", oldStylePrefs.unreadTab);
  720.       browserWindow.gTMPprefObserver.converOldStylePrefs("progressMeter", oldStylePrefs.progressMeter);
  721.       oldStylePrefs = {currentTab: {}, unreadTab: {}, progressMeter: {}, found: false};
  722.    }
  723.    gTabmixPrefs.clearUserPref("extensions.tabmix.setDefault");
  724.  
  725.    TM_setElements(false);
  726.  
  727.    TM_disableApply();
  728.  
  729.    TM_Options.isSessionStoreEnabled(true);
  730.    callUpdateSettings();
  731.  
  732.    nsIPrefServiceObj.savePrefFile(null); // store the pref immediately
  733.  
  734.    return true;
  735. }
  736.  
  737. function loadFromFile() {
  738.    // thanks to adblock
  739.    var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  740.    var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
  741.    var streamIO = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
  742.  
  743.    fp.init(window, null, fp.modeOpen);
  744.    fp.defaultExtension = "txt";
  745.    fp.appendFilters(fp.filterText);
  746.  
  747.    if (fp.show() != fp.returnCancel) {
  748.       stream.init(fp.file, 0x01, 0444, null);
  749.       streamIO.init(stream);
  750.       var input = streamIO.read(stream.available());
  751.       streamIO.close();
  752.       stream.close();
  753.  
  754.       var linebreak = input.match(/(((\n+)|(\r+))+)/m)[1]; // first: whole match -- second: backref-1 -- etc..
  755.       return input.split(linebreak);
  756.    }
  757.    return null;
  758. }
  759.  
  760. function convertSession() {
  761.    var browserWindow = getMostRecentWindow();
  762.    if (document.getElementById("chooseFile").selectedItem.value == 0)
  763.       browserWindow.convertSession.selectFile(window);
  764.    else
  765.       browserWindow.convertSession.convertFile();
  766.  
  767.    this.focus();
  768. }
  769.  
  770. var applyData = [];
  771. function TM_enableApply(aEvent) {
  772.    var item = aEvent.target;
  773.  
  774.    // only allow event from this item to go on....
  775.    var n = item.localName;
  776.    if (n != "radio" && n != "menuitem" &&
  777.               n != "checkbox" && n != "textbox")
  778.       return;
  779.  
  780.    if (n == "checkbox" && document.getElementById("obs_" + item.id))
  781.      TM_Options.disabled(item);
  782.  
  783.    // if we fail once no need to continue and we keep the apply button enable
  784.    if ("undefined" in applyData)
  785.       return;
  786.  
  787.    // set item for menuitem
  788.    if (n == "menuitem")
  789.      item = item.parentNode.parentNode;
  790.    // set item for radio
  791.    if (n == "radio")
  792.      item = item.parentNode;
  793.  
  794.    if (item.hasAttribute("prefstring_item")) {
  795.      var itemId = item.getAttribute("prefstring_item");
  796.      if (itemId == "no_prefstring")
  797.        return;
  798.      else {
  799.        item = document.getElementById(itemId);
  800.        item.value = aEvent.target.value; // we don't use this for checkbox
  801.      }
  802.    }
  803.  
  804.    // fix "-" in ss_postdatabytes to allow "-1"
  805.    if (item.id == "ss_postdatabytes" && item.value.length > 1) {
  806.       var val = item.value;
  807.  
  808.       if (val.length == 2 && val.indexOf("-") == 0 && val != "-1")
  809.          aEvent.target.value = "-1";
  810.  
  811.       if (val.length > 2 && val.indexOf("-") == 0)
  812.          aEvent.target.value = val.substr(2);
  813.    }
  814.  
  815.    if (item.hasAttribute("prefstring"))
  816.       updateApplyData(item);
  817.    else {
  818.       tmLog("erorr in tabmix options, item.id " + aEvent.target.id + "\n has no prefstring");
  819.       applyData["undefined"] = true;
  820.       document.documentElement.getButton("extra1").disabled = false;
  821.    }
  822. }
  823.  
  824. // return value from item
  825. function getValue(item) {
  826.    if (item.localName == "checkbox")
  827.       return item.hasAttribute("inverted") ? !item.checked : item.checked;
  828.  
  829.    return item.value;
  830. }
  831.  
  832. // set value to item
  833. function setValue(item, newValue) {
  834.    if (item.localName == "checkbox")
  835.       item.checked = item.hasAttribute("inverted") ? !newValue : newValue;
  836.    else
  837.       item.value = newValue;
  838. }
  839.  
  840. function updateApplyData(item, newValue) {
  841.    if (typeof(newValue) == "undefined")
  842.      newValue = getValue(item);
  843.    else
  844.      setValue(item, newValue)
  845.  
  846.    var pref = item.getAttribute("prefstring");
  847.    var savedValue = getPrefByType(pref);
  848.  
  849.    if (savedValue != newValue)
  850.       applyData[pref] = newValue;
  851.    else if (pref in applyData)
  852.       delete applyData[pref];
  853.  
  854.    var applyCount = 0;
  855.    for (var n in applyData) {
  856.       if (++applyCount > 0)
  857.          break;
  858.    }
  859.  
  860.    var applyButton = document.documentElement.getButton("extra1");
  861.    if (applyButton.disabled != (applyCount == 0))
  862.       applyButton.disabled = applyCount == 0;
  863. }
  864.  
  865. function TM_disableApply() {
  866.    document.documentElement.getButton("extra1").disabled = true;
  867.    applyData = [];
  868. }
  869.  
  870. function setLastTab(event) {
  871. try {
  872.    // remove EventListener when we exit
  873.    window.removeEventListener("unload", setLastTab, false);
  874.    window.removeEventListener("command", TM_enableApply, false);
  875.    window.removeEventListener("input", TM_enableApply, false);
  876.    document.getElementById("tabclicking_tabs").removeEventListener("select", tabSelectionChanged, false);
  877.  
  878.    gTabmixPrefs.setIntPref("extensions.tabmix.selected_tab", document.getElementById("tabMixTabBox").selectedIndex);
  879.    var subtabs = document.getElementsByAttribute("subtub", "true");
  880.    var subTab = "extensions.tabmix.selected_sub_tab";
  881.    for (var i = 0; i < subtabs.length; i++)
  882.       gTabmixPrefs.setIntPref(subTab + subtabs[i].getAttribute("value"), subtabs[i].selectedIndex);
  883.  
  884.   nsIPrefServiceObj.savePrefFile(null); // store the pref immediately
  885.  
  886. } catch(ex) {}
  887. }
  888.  
  889. function getTab() {
  890. try {
  891.    var browserWindow = getMostRecentWindow();
  892.    var selTabindex = browserWindow.TMP_getIntPref("", "extensions.tabmix.selected_tab" , 0);
  893.    TM_selectTab(selTabindex);
  894.  
  895.    var subtabs = document.getElementsByAttribute("subtub", true);
  896.    var subTab = "extensions.tabmix.selected_sub_tab";
  897.    for (var i = 0; i < subtabs.length; i++) {
  898.       var val = browserWindow.TMP_getIntPref("" ,subTab + subtabs[i].getAttribute("value"), 0);
  899.       subtabs[i].selectedIndex = val;
  900.    }
  901. } catch(ex) {tmLog(ex);}
  902. }
  903.  
  904. // this function is called from here and from TMP_openDialog if the dialog already opened
  905. function TM_selectTab(aSelTab) {
  906.   document.getElementById("tabMixTabBox").selectedIndex = (aSelTab) ? aSelTab : 0;
  907.   var tabId = document.getElementsByTagName("tab")[aSelTab].id;
  908.   var catButtons = document.getElementById("TM_ButtonBox").childNodes;
  909.  
  910.   for(var i = 0; i < catButtons.length; i++)
  911.     if(catButtons[i].getAttribute("group", "categories"))
  912.       catButtons[i].setAttribute("checked", (catButtons[i].id == "button" + tabId));
  913. }
  914.  
  915. function showIncompatible() {
  916.    var browserWindow = getMostRecentWindow();
  917.    if (browserWindow) {
  918.       var result = browserWindow.disableExtensions(this);
  919.       if (result) {
  920.          document.getElementById("incompatible").collapsed = true;
  921.          sizeToContent();
  922.       }
  923.       this.focus();
  924.    }
  925.    else {
  926.       var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"]
  927.                                     .getService(Ci.nsIPromptService);
  928.       var stringBundle = document.getElementById("tmp-string-bundle");
  929.       var title = stringBundle.getString("tabmixoption.error.title");
  930.       var msg = stringBundle.getString("tabmixoption.error.msg");
  931.       promptService.alert(window, title, msg);
  932.       window.close();
  933.    }
  934. }
  935.  
  936. function tabSelectionChanged(event) {
  937.    if (!event || event.target.localName != "tabs")
  938.       return;
  939.  
  940.    var index = event.target.selectedIndex;
  941.    setSelectedIndex(index);
  942. }
  943.  
  944. function setSelectedIndex(index) {
  945.    var c = ["dbl", "middle", "ctrl", "shift", "alt"];
  946.    var clickTab = document.getElementById("ClickTab");
  947.    var prefId = c[index] + "ClickTab";
  948.    clickTab.value = document.getElementById(prefId).value;
  949.    clickTab.setAttribute("prefstring_item", prefId);
  950.  
  951.    var clickTabbar = document.getElementById("ClickTabbar");
  952.    prefId = c[index] + "ClickTabbar";
  953.    clickTabbar.value = document.getElementById(prefId).value;
  954.    clickTabbar.setAttribute("prefstring_item", prefId);
  955. }
  956.  
  957. function getMostRecentWindow() {
  958.   return winMediator.getMostRecentWindow("navigator:browser");
  959. }
  960.  
  961. function tabmixCustomizeToolbar() {
  962.   window._tabmixCustomizeToolbar = true;
  963.   getMostRecentWindow().BrowserCustomizeToolbar();
  964. }
  965.  
  966. function toolbarButtons(aWindow) {
  967.   var buttons = ["btn_sessionmanager", "btn_undoclose", "btn_closedwindows", "btn_tabslist"];
  968.   var onToolbar = document.getElementById("onToolbar");
  969.   var onPlate = document.getElementById("onPlate");
  970.   for (var i = 0; i < buttons.length; ++i ) {
  971.     var button = aWindow.document.getElementById(buttons[i]);
  972.     var optionButton = document.getElementById("_" + buttons[i]).parentNode;
  973.     if (button)
  974.       onToolbar.appendChild(optionButton);
  975.     else
  976.       onPlate.appendChild(optionButton);
  977.   }
  978.   onToolbar.childNodes[1].hidden = onToolbar.childNodes.length > 2;
  979.   onPlate.childNodes[1].hidden = onPlate.childNodes.length > 2;
  980. }
  981.  
  982. function openHelp() {
  983.   var helpPages = [{id:"Links",   tabs:[""]},
  984.                    {id:"Events",  tabs:["Tab_Opening", "Tab_Focus", "Tab_Closing", "Tab_Merging", "Tab_Features"]}, // 1
  985.                    {id:"Display", tabs:["Tab_bar", "Tab" , "Tab"]}, /* toolbar not exsit yet */                     // 2
  986.                    {id:"Mouse",   tabs:["Mouse_Gestures", "Mouse_Clicking"]},                                       // 3 //sub sub tab 6
  987.                    {id:"Menu",    tabs:["Tab_Context_Menu", "Main_Context_Menu", "Tools_Menu"]},                    // 4
  988.                    {id:"Session", tabs:["StartExit", "Restore" , "Preserve"]},                                      // 5
  989.                   ];
  990.   // get curent tab index and sub tab if there is one
  991.   var topLevel, subLevel;
  992.   topLevel = subLevel = document.getElementById("tabMixTabBox").selectedIndex;
  993.   var subtabs = document.getElementsByAttribute("subtub", "true");
  994.   if (topLevel > 0) {
  995.     if (topLevel < 4)
  996.       subLevel--;
  997.     subLevel = subtabs[subLevel].selectedIndex
  998.   }
  999.   var subPage = helpPages[topLevel].tabs[subLevel];
  1000.   var pageaddress = "/#" + helpPages[topLevel].id + (subPage ? "_-_" + subPage : "");
  1001.   var helpPage = "http://tmp.garyr.net/help"
  1002.   var helpUrl = helpPage + pageaddress;
  1003.   var tabToSelect;
  1004.   // Check if the help page already open
  1005.   var recentWindow = getMostRecentWindow();
  1006.   var tabBrowser = recentWindow.gBrowser;
  1007.   for (var i = 0; i < tabBrowser.browsers.length; i++) {
  1008.     if (tabBrowser.browsers[i].currentURI.spec.indexOf(helpPage) == 0) {
  1009.       tabToSelect = tabBrowser.tabContainer.childNodes[i];
  1010.       break;
  1011.     }
  1012.   }
  1013.   if (!tabToSelect) {
  1014.     if (tabBrowser.isBlankNotBusyTab(tabBrowser.mCurrentTab))
  1015.       tabToSelect = tabBrowser.mCurrentTab
  1016.     else
  1017.       tabToSelect = tabBrowser.addTab("about:blank");
  1018.   }
  1019.   tabBrowser.selectedTab = tabToSelect;
  1020.   tabBrowser.selectedBrowser.userTypedValue = helpUrl;
  1021.   recentWindow.loadURI(helpUrl, null, null, false);
  1022.   if (recentWindow.gIsFirefox36)
  1023.     tabBrowser.selectedBrowser.focus();
  1024.   else
  1025.     recentWindow.focusElement(recentWindow.content);
  1026. }
  1027.